
#include "kv.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>


#define tc_log(_fmt_, ...)          printf("[TC][%s:%d][%s] "_fmt_"\r\n", __FILE__, __LINE__, __func__, ##__VA_ARGS__)
#define tc_log_err(_fmt_, ...)      printf("[TC][ERR][%s:%d][%s] "_fmt_"\r\n", __FILE__, __LINE__, __func__, ##__VA_ARGS__)


#define KV_HEAP_LENGTH			512


static void hex_dump(char *desc, unsigned char *data, int len)
{
    unsigned int i;

    printf("%s size %d hex dump:", desc, len);
    for(i=0; i<len; i++)
    {
        if (i % 16 == 0)
            printf("\n0x%p: ", &data[i]);

        printf(" %02x", data[i]);
    }
    printf("\n");
}

int main()
{
	kv_obj_t kv_ram;
	int ret, i;
	unsigned char set_buff[16], get_buff[16];

	tc_log("********************");
	tc_log("this is a new start.");
	tc_log("********************");

	unsigned char *kv_heap = calloc(KV_HEAP_LENGTH, sizeof(unsigned char));
	kv_ram.heap = kv_heap;
	kv_ram.heap_size = KV_HEAP_LENGTH;
	kv_init(&kv_ram);

	for (i=0; i<sizeof(set_buff); ++i)
		set_buff[i] = i;

	ret = kv_set(&kv_ram, "aa", set_buff, sizeof(set_buff)/2);
	tc_log("kv_set ret %d", ret);

	memset(get_buff, 0x0, sizeof(get_buff));
	ret = kv_get(&kv_ram, "aa", get_buff, sizeof(get_buff));
	tc_log("kv_get ret %d", ret);
	hex_dump("1. get_buff", get_buff, sizeof(get_buff));

	for (i=0; i<sizeof(set_buff); ++i)
		set_buff[i] = sizeof(set_buff) - i;

	ret = kv_set(&kv_ram, "aa", set_buff, sizeof(set_buff));
	tc_log("kv_set ret %d", ret);

	memset(get_buff, 0x0, sizeof(get_buff));
	ret = kv_get(&kv_ram, "aa", get_buff, sizeof(get_buff));
	tc_log("kv_get ret %d", ret);
	hex_dump("2. get_buff", get_buff, sizeof(get_buff));

	ret = kv_set(&kv_ram, "tag.debug", "kv debug", strlen("kv debug"));
	tc_log("kv_set ret %d", ret);

	ret = kv_set(&kv_ram, "tag.release", "kv release", strlen("kv release"));
	tc_log("kv_set ret %d", ret);

	ret = kv_del(&kv_ram, "aa");
	tc_log("kv_del ret %d", ret);

	ret = kv_del(&kv_ram, "tag.debug");
	tc_log("kv_del ret %d", ret);

	ret = kv_del(&kv_ram, "tag.release");
	tc_log("kv_del ret %d", ret);

	free(kv_heap);
	tc_log("exit");
	return 0;
}

